package cn.hdustea.shopNavigation;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiDetailSearchResult;
import com.baidu.mapapi.search.poi.PoiIndoorInfo;
import com.baidu.mapapi.search.poi.PoiIndoorOption;
import com.baidu.mapapi.search.poi.PoiIndoorResult;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.baidu.mapapi.search.route.IndoorPlanNode;

import java.util.ArrayList;
import java.util.List;

import cn.hdustea.shopNavigation.adapter.PoiSearchAdapter;
import cn.hdustea.shopNavigation.entity.IndoorPlanBean;


public class NavigationActivity extends AppCompatActivity {

    private Button buttonCalculateRouter = null;
    private Button buttonReset = null;
    private Button buttonBack2 = null;
    private EditText editTextStartPoint = null;
    private EditText editTextEndPoint = null;
    private CardView searchPoiCard = null;
    private CardView cardCalculateRouter = null;
    private ConstraintLayout searchPoiLayout = null;
    private RecyclerView recyclerViewSearchPoi = null;
    private IndoorPlanBean startNodeBean = null;
    private IndoorPlanBean endNodeBean = null;
    private boolean startByMyPosition = true;
    private boolean endByMyPosition = false;
    private PoiSearchAdapter poiSearchAdapter = null;
    private ImageView imageViewVoiceStart = null;
    private ImageView imageViewVoiceEnd = null;
    private TextWatcher textWatcher = null;
    private Button buttonMyPosition = null;
    private InputMethodManager manager = null;
    private String bid = null;
    private PoiSearch poiSearch = null;
    private OnGetPoiSearchResultListener tipListener = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation);
        manager = (InputMethodManager) getSystemService(NavigationActivity.this.INPUT_METHOD_SERVICE);
        initAdapters();
        initViews();
        initListeners();
        if (getIntent().getExtras() != null) {
            bid = getIntent().getExtras().getString("bid");
            IndoorPlanBean endNodeBean = (IndoorPlanBean) getIntent().getExtras().getParcelable("endNodeBean");
            if (endNodeBean != null) {
                this.endNodeBean = endNodeBean;
                System.out.println(endNodeBean.getLocation());
                editTextEndPoint.setText(endNodeBean.getName());
            }
        }
        setCalculateCard();
    }

    /**
     *
     * @param keyword
     */
    private void searchPoi(String keyword) {
        if (!keyword.isEmpty()) {
            // bid -室内id wd- 室内poi检索关键字
            poiSearch.searchPoiIndoor(new PoiIndoorOption().poiIndoorBid(bid).poiIndoorWd(keyword));
        } else {
            poiSearchAdapter.setPoiIndoorInfos(new ArrayList<PoiIndoorInfo>());
        }
    }


    private void setCalculateCard() {
        if ((startNodeBean != null || startByMyPosition == true) && (endNodeBean != null || endByMyPosition == true)) {
            cardCalculateRouter.setVisibility(View.VISIBLE);
        } else {
        }
    }

    private void initViews() {
        buttonCalculateRouter = findViewById(R.id.buttonCalculateRouter);
        searchPoiCard = findViewById(R.id.searchPoiCard);
        recyclerViewSearchPoi = findViewById(R.id.recyclerViewSearchPoi);
        cardCalculateRouter = findViewById(R.id.cardCalculateRouter);
        buttonBack2 = findViewById(R.id.buttonBack2);
        buttonMyPosition = findViewById(R.id.buttonMyPosition);
        buttonReset = findViewById(R.id.buttonReset);
        editTextStartPoint = findViewById(R.id.editTextStartPoint);
        editTextEndPoint = findViewById(R.id.editTextEndPoint);
        imageViewVoiceStart = findViewById(R.id.imageViewVoiceStart);
        imageViewVoiceEnd = findViewById(R.id.imageViewVoiceEnd);
        searchPoiLayout = findViewById(R.id.searchPoiLayout);

        searchPoiLayout.setVisibility(View.GONE);
        cardCalculateRouter.setVisibility(View.GONE);

        recyclerViewSearchPoi.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        recyclerViewSearchPoi.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        recyclerViewSearchPoi.setItemAnimator(new DefaultItemAnimator());

        recyclerViewSearchPoi.setAdapter(poiSearchAdapter);

        poiSearch = PoiSearch.newInstance();
    }

    private void initAdapters() {
        poiSearchAdapter = new PoiSearchAdapter(NavigationActivity.this, new ArrayList<PoiIndoorInfo>());
    }

    private void initListeners() {
        tipListener = new OnGetPoiSearchResultListener() {
            @Override
            public void onGetPoiResult(PoiResult poiResult) {
            }

            @Override
            public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {

            }

            @Override
            public void onGetPoiDetailResult(PoiDetailSearchResult poiDetailSearchResult) {
//                System.out.println(poiDetailSearchResult);

            }

            @Override
            public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {
                if (poiIndoorResult.getArrayPoiInfo() != null) {
                    poiSearchAdapter.setPoiIndoorInfos(poiIndoorResult.getArrayPoiInfo());
                } else {
                    poiSearchAdapter.setPoiIndoorInfos(new ArrayList<PoiIndoorInfo>());
                }
            }
        };
        poiSearch.setOnGetPoiSearchResultListener(tipListener);
        textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                searchPoi(s.toString());
            }
        };
        /**
         * 计算路径
         */
        buttonCalculateRouter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("startNodeBean", startNodeBean);
                intent.putExtra("endNodeBean", endNodeBean);
                intent.putExtra("startByMyPosition", startByMyPosition);
                intent.putExtra("endByMyPosition", endByMyPosition);
                setResult(Activity.RESULT_OK, intent);
                finish();
            }
        });
        buttonReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        buttonBack2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        // 开始位置设置
        editTextStartPoint.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    searchPoi(editTextStartPoint.getText().toString());
                    editTextStartPoint.addTextChangedListener(textWatcher);
                    buttonMyPosition.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            startByMyPosition = true;
                            editTextStartPoint.removeTextChangedListener(textWatcher);
                            editTextStartPoint.setText("我的位置");
                            editTextStartPoint.clearFocus();
                        }
                    });
                    searchPoiLayout.setVisibility(View.VISIBLE);
                    cardCalculateRouter.setVisibility(View.GONE);
                    poiSearchAdapter.setOnItemClickListener(new PoiSearchAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(View view, int position) {
                            List<PoiIndoorInfo> poiIndoorInfos = poiSearchAdapter.getPoiIndoorInfos();
                            PoiIndoorInfo poiIndoorInfo = poiIndoorInfos.get(position);
                            startNodeBean = new IndoorPlanBean(poiIndoorInfo.uid, poiIndoorInfo.latLng, poiIndoorInfo.floor, poiIndoorInfo.name);
                            startByMyPosition = false;
                            editTextStartPoint.removeTextChangedListener(textWatcher);
                            editTextStartPoint.setText(startNodeBean.getName());
                            editTextStartPoint.clearFocus();
                        }
                    });
                } else {
                    editTextStartPoint.removeTextChangedListener(textWatcher);
                    if (!editTextEndPoint.hasFocus()) {
                        manager.hideSoftInputFromWindow(editTextStartPoint.getWindowToken(), 0);
                        searchPoiLayout.setVisibility(View.GONE);
                        setCalculateCard();
                    }
                }
            }
        });

        // TODO: 2020-10-31
        editTextEndPoint.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            /**
             * v 发生变化的视图， hasFocus 判断视图是否获得了焦点
             */
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    searchPoi(editTextEndPoint.getText().toString());
                    editTextEndPoint.addTextChangedListener(textWatcher);
                    buttonMyPosition.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            endByMyPosition = true;
                            editTextStartPoint.removeTextChangedListener(textWatcher);
                            editTextEndPoint.setText("我的位置");
                            editTextEndPoint.clearFocus();
                        }
                    });
                    searchPoiLayout.setVisibility(View.VISIBLE);
                    cardCalculateRouter.setVisibility(View.GONE);
                    poiSearchAdapter.setOnItemClickListener(new PoiSearchAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(View view, int position) {
                            List<PoiIndoorInfo> poiIndoorInfos = poiSearchAdapter.getPoiIndoorInfos();
                            PoiIndoorInfo poiIndoorInfo = poiIndoorInfos.get(position);
                            endNodeBean = new IndoorPlanBean(poiIndoorInfo.uid, poiIndoorInfo.latLng, poiIndoorInfo.floor, poiIndoorInfo.name);
                            endByMyPosition = false;
                            editTextEndPoint.removeTextChangedListener(textWatcher);
                            editTextEndPoint.setText(endNodeBean.getName());
                            editTextEndPoint.clearFocus();
                        }
                    });
                } else {
                    editTextEndPoint.removeTextChangedListener(textWatcher);
                    if (!editTextStartPoint.hasFocus()) {
                        manager.hideSoftInputFromWindow(editTextEndPoint.getWindowToken(), 0);
                        searchPoiLayout.setVisibility(View.GONE);
                        setCalculateCard();
                    }
                }
            }
        });
    }

}